//
//  squareRightMenuView.m
//  FastRunDemo
//
//  Created by 苟亚明 on 16/9/28.
//  Copyright © 2016年 苟亚明. All rights reserved.
//

#import "squareRightMenuView.h"
#import "rightPullMenuTable.h"
#define rightMenuViewW GYMScreenW / 2
@interface squareRightMenuView() <UIGestureRecognizerDelegate>
//required
@property (nonatomic, strong) UIViewController *ContainerVC;


@end
@implementation squareRightMenuView
// 单利对象
static squareRightMenuView *menuView = nil;
+ (instancetype)ShareRightMenuView{
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        menuView = [[self alloc] initWithFrame:CGRectMake(GYMScreenW, 64, rightMenuViewW, GYMScreenH - 64 - 49)];
    });
    return menuView;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        menuView = [super allocWithZone:zone];
    });
    return menuView;
}
- (instancetype)initWithCotainerViewController:(UIViewController *)containerVC{
    if (self = [super init]) {
        
        // 获取控制器
        _ContainerVC = containerVC;
        // 开始隐藏当前View
        self.isRightViewHidden = YES;
        
        // 初始化遮罩view
        _maskView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        
        _maskView.backgroundColor = [UIColor blackColor];
        
        _maskView.hidden = YES;
        
        // 添加遮罩View到控制器的view中
        [_ContainerVC.view addSubview:_maskView];
        
        // 注册观察着模式
        [self addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        
        // 添加手势
        [self addRecognizer];
    }
    return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{
    
    if ([keyPath isEqualToString:@"frame"]) {
        
        CGRect new = [change[@"new"] CGRectValue];
        CGFloat x = new.origin.x;
        
        if (x != GYMScreenW) {
            _maskView.hidden = NO;
            // 设置遮罩的透明度
            
            _maskView.alpha = (x - GYMScreenW)/ (-rightMenuViewW) * 0.5;
        }
        if (self.x < GYMScreenW) {
            self.isShowInView = YES;
        }else{
            self.isShowInView = NO;
        }
        
    }
}
#pragma mark - UIPanGestureRecognizer
-(void)addRecognizer{
    
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(didPanEvent:)];
    
    pan.delegate = self;
    [_ContainerVC.view addGestureRecognizer:pan];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeLeftViewEvent:)];
    [_maskView addGestureRecognizer:tap];
    
}

// 遮罩view点击事件
- (void)closeLeftViewEvent:(UITapGestureRecognizer *)recognizer{
    [self closeRightMenuView];
}

-(void)didPanEvent:(UIPanGestureRecognizer *)recognizer{
    
    CGPoint translation = [recognizer translationInView:_ContainerVC.view];
    
    [recognizer setTranslation:CGPointZero inView:_ContainerVC.view];

    
    if(UIGestureRecognizerStateBegan == recognizer.state ||
       UIGestureRecognizerStateChanged == recognizer.state){

        
        if (translation.x < 0) {//SwipLeft
            
            if (self.x == rightMenuViewW) {
                return;
            }
            CGFloat tempX = self.x + translation.x;
            if (tempX >= rightMenuViewW) {
                
                self.frame = CGRectMake(tempX, 64, rightMenuViewW, GYMScreenH - 64 - 49);
                
            }else{
                
                self.frame = CGRectMake(rightMenuViewW, 64, rightMenuViewW, GYMScreenH - 64 - 49);
            }
            
        }else{//SwipRight
            
            CGFloat tempX = self.x + translation.x;
            self.frame = CGRectMake(tempX, 64, rightMenuViewW, GYMScreenH - 64 - 49);
        }
        
    }else{
        NSLog(@"shoushi ting zhi");
        
        if (self.x <= (GYMScreenW / 4) * 3) {
            
            [self openRightMenuView];
            
        }else{
            
            [self closeRightMenuView];
        }
    }
}

/**
 *  关闭右视图
 */
- (void)closeRightMenuView{
    
    NSLog(@"closeRightView");
 
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.frame = CGRectMake(GYMScreenW, 64, rightMenuViewW, GYMScreenH - 64 - 49);
        self.isRightViewHidden = YES;
        _maskView.hidden = YES;
    }];
    
}
/**
 *  开启右视图
 */
- (void)openRightMenuView{

    NSLog(@"openLeftView");
    [UIView animateWithDuration:0.3 animations:^{
        
        self.frame = CGRectMake(rightMenuViewW, 64, rightMenuViewW, GYMScreenH - 64 - 49);
        self.isRightViewHidden = NO;
    } completion:^(BOOL finished) {
        
        _maskView.hidden = NO;
        _maskView.alpha = 0.5;
    }];
}


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    
    if ([otherGestureRecognizer.view isKindOfClass:[UITableView class]] || [otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) {
        
        return NO;
    }
    
    return YES;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognize{
    if ([gestureRecognize locationInView:_ContainerVC.view].x <= GYMScreenW/4 * 3) {
        return NO;
    }
    return YES;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        rightPullMenuTable *table = [rightPullMenuTable viewFromNib];
        table.frame = CGRectMake(0, 0, rightMenuViewW, GYMScreenH - 64 - 49);
        [self addSubview:table];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self removeObserver:self forKeyPath:@"frame"];
}

@end
